<?php
	require_once("lib/includes.php");

	DB::Connect();
?>

<html>
<head>
	<title>Open 20 questions</title>

	<link href="css/site.css" rel="stylesheet" type="text/css" />
	<link href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />

	<script src="jquery/jquery-1.10.2.min.js" type="text/javascript"></script>
	<script src="jquery/jquery-ui-1.10.3.js" type="text/javascript"></script>

	<script src="jquery/jquery.tmpl.min.js" type="text/javascript"></script>
	<script src="jquery/jquery.tmplPlus.min.js" type="text/javascript"></script>
	<script src="lib/plugins/JSON.js" type="text/javascript"></script>
	<script src="lib/plugins/PreventSubmit.js"></script>
	<script src="lib/plugins/PHPWebPage.js"></script>


	<style type="text/css">
		#divObjectList .column1
		{
			float:left;
			width:50px;
		}

		#divObjectList .column2
		{
			float:left;
			width:250px;
		}

		#divObjectList .column3
		{
			float:left;
			width:120px;
		}

	</style>

</head>

<body>
	<div class="wrapper">

		<h1>20 questions game</h1>

		<?php

		$page_size = 1; // page size
		$page_index = isset($_GET['page']) ? $_GET['page'] : 1; // gets the page index

		$page = '?';

		// gets the total number of objects currently available in the database
		// we use this to build up the pager
		$totalItemsCount = Queries::GetObjectsCount();

		$objectList = Queries::GetObjectsList($page_index, $page_size);


		$page_count = ($totalItemsCount + $page_size - 1) / $page_size;

		?>

		<div class="clear"></div>
		<div>
			<?php

			if ($page_index == 0 || $page_index > $page_count)
				$page_index = 1;

			$stop = $page_index > 5 ? 5 : $page_index;
			for ($i = 1; $i <= $stop; $i++)
			{
				if ($i != $page_index)
				{
					echo '<a class="noVisited" href="' . $page . 'page=' . $i . '">' . $i . '</a> ';
				}
			}

			// 5 before
			$i = $page_index - 5;
			if ($i > 6)
			{
				echo ' ... ';
			}

			while ($i <= 5)
				$i++;
			for (; $i < $page_index; $i++)
			{
				echo '<a class="noVisited" href="' . $page . 'page=' . $i . '">' . $i . '</a> ';
			}

			// current
			echo '<b>[ ' . $page_index . ' ]</b> ';

			// 5 after
			for ($i = $page_index + 1; $i <= $page_count && $i - $page_index <= 5; $i++)
			{
				echo '<a class="noVisited" href="' . $page . 'page=' . $i . '">' . $i . '</a> ';
			}

			// last 5
			$i = $page_count - 4;
			if ($i > $page_index + 6)
			{
				echo ' ... ';
			}

			while ($i <= $page_index + 5)
				$i++;
			for (; $i <= $page_count; $i++)
			{
				echo '<a class="noVisited" href="' . $page . 'page=' . $i . '">' . $i . '</a> ';
			}
			?>
		</div>
		<div class="clear"></div>



		<div class="records" id="divObjectList">
			<div class="header">
				<div class="column1">
					#
				</div>

				<div class="column2">
					Name
				</div>

				<div class="column3">
					Actions
				</div>

				<div class="clear"></div>
			</div>
			<div class="clear"></div>



			<?php

				$array_length = count($objectList);

			foreach($objectList as $key => $values)
			{
				echo
					'<div class="record">'.
						'<div class="column1">'.
							(($page_index - 1) * $page_size + $key + 1).'.'.
						'</div>'.

						'<div class="column2">'.
							$values['ObjectName'].
						'</div>'.

					'<div class="column3">'.
						'<a href="objectquestions.php?objectid='.$values['ObjectId'].'" data-id="'.$values['ObjectId'].'" title="Answer questions">Answer questions</a>'.
					'</div>'.

					'<div class="clear"></div>'.
				'</div>';
			}
			?>

		</div>

		<div class="clear"></div>


		<div>
			<?php

				if ($page_index == 0 || $page_index > $page_count)
					$page_index = 1;

				$stop = $page_index > 5 ? 5 : $page_index;
				for ($i = 1; $i <= $stop; $i++)
				{
					if ($i != $page_index)
					{
						echo '<a class="noVisited" href="' . $page . 'page=' . $i . '">' . $i . '</a> ';
					}
				}

				// 5 before
				$i = $page_index - 5;
				if ($i > 6)
				{
					echo ' ... ';
				}

				while ($i <= 5)
				$i++;
				for (; $i < $page_index; $i++)
				{
					echo '<a class="noVisited" href="' . $page . 'page=' . $i . '">' . $i . '</a> ';
				}

				// current
				echo '<b>[ ' . $page_index . ' ]</b> ';

				// 5 after
				for ($i = $page_index + 1; $i <= $page_count && $i - $page_index <= 5; $i++)
				{
				echo '<a class="noVisited" href="' . $page . 'page=' . $i . '">' . $i . '</a> ';
				}

				// last 5
				$i = $page_count - 4;
				if ($i > $page_index + 6)
				{
					echo ' ... ';
				}

				while ($i <= $page_index + 5)
				$i++;
				for (; $i <= $page_count; $i++)
				{
					echo '<a class="noVisited" href="' . $page . 'page=' . $i . '">' . $i . '</a> ';
				}
			?>
		</div>




	</div>

</body>
</html>

<?php
	DB::Disconnect();
?>